package com.fmdj.mis.api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.PageUtils;
import com.fmdj.mis.api.controller.form.*;
import com.fmdj.mis.api.service.VoucherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;

@RestController
@RequestMapping("/voucher")
@Tag(name = "VoucherController", description = "代金卷Web接口")
@Slf4j
public class VoucherController {

    @Autowired
    private VoucherService voucherService;
    @PostMapping("/insertVoucher")
    @Operation(summary = "添加代金卷")
    @SaCheckPermission(value = {"ROOT", "VOUCHER:INSERT"}, mode = SaMode.OR)
    public CommonResult insertVoucher(@RequestBody @Valid InsertVoucherForm form) {
        form.setStatus((byte)3); //默认为未上架
        int rows = voucherService.insertVoucher(form);
        return CommonResult.ok().put(CommonResult.RETURN_ROW, rows);
    }

    @PostMapping("/searchVoucherByPage")
    @Operation(summary = "查询代金卷")
    @SaCheckPermission(value = {"ROOT", "VOUCHER:SELECT"}, mode = SaMode.OR)
    public CommonResult searchVoucherByPage(@RequestBody @Valid SearchVoucherByPageForm form) {
        PageUtils pageUtils = voucherService.searchVoucherByPage(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, pageUtils);
    }

    @PostMapping("/updateVoucherStatus")
    @Operation(summary = "更新代金卷的状态")
    @SaCheckPermission(value = {"ROOT", "VOUCHER:UPDATE"}, mode = SaMode.OR)
    public CommonResult updateVoucherStatus(@RequestBody @Valid UpdateVoucherStatusForm form) {
        int rows = voucherService.updateVoucherStatus(form);
        return CommonResult.ok().put("rows", rows);
    }

    @PostMapping("/deleteVoucherByIds")
    @Operation(summary = "删除代金卷")
    @SaCheckPermission(value = {"ROOT", "VOUCHER:DELETE"}, mode = SaMode.OR)
    public CommonResult deleteVoucherByIds(@RequestBody @Valid DeleteVoucherByIdsForm form) {
        int rows = voucherService.deleteVoucherByIds(form);
        return CommonResult.ok().put("rows", rows);
    }
}
